package com.syzg.common.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.Resource;

public class MyProperties extends Properties
{
  private static final long serialVersionUID = 1L;

  public MyProperties()
  {
  }

  public MyProperties(Resource location)
    throws IOException
  {
    load(location.getInputStream());
  }

  public static MyProperties getInstance(String location)
  {
    MyProperties myProperties = new MyProperties();
    if ((location == null) || (location.length() <= 0)) {
      return null;
    }
    InputStream inputStream = null;
    if (location.toLowerCase().startsWith("classpath:")) {
      String path = location.substring("classpath:".length());
      if (!path.startsWith("/")) {
        path = "/" + path;
      }
      inputStream = MyProperties.class.getResourceAsStream(path);
    } else {
      if (!location.startsWith("/"))
        location = "/" + location;
      inputStream = MyProperties.class.getResourceAsStream(location);
    }
    try
    {
      myProperties.load(inputStream);
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
    if (inputStream != null) {
      try
      {
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    if (inputStream != null) {
      try
      {
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return myProperties;
  }

  public MyProperties(Resource[] locations)
    throws IOException
  {
    Resource[] aresource;
    int j = (aresource = locations).length;
    for (int i = 0; i < j; i++) {
      Resource location = aresource[i];
      load(location.getInputStream());
    }
  }

  public void load(Resource location) throws IOException
  {
    load(location.getInputStream());
  }

  public boolean getProperty(String name, boolean defaultValue) {
    String value = getProperty(name);
    return value != null ? Boolean.valueOf(value).booleanValue() : defaultValue;
  }

  public boolean getBooleanProperty(String name) {
    return getBooleanProperty(name, false);
  }

  public boolean getBooleanProperty(String name, boolean defaultValue) {
    return getProperty(name, defaultValue);
  }

  public int getProperty(String name, int defaultValue) {
    String value = getProperty(name);
    return value != null ? Integer.parseInt(value) : defaultValue;
  }

  public int getIntProperty(String name) {
    return getIntProperty(name, 0);
  }

  public int getIntProperty(String name, int defaultValue) {
    return getProperty(name, defaultValue);
  }

  public Integer getProperty(String name, Integer defaultValue) {
    String value = getProperty(name);
    return value != null ? Integer.valueOf(name) : defaultValue;
  }

  public Integer getIntegerProperty(String name) {
    return getIntegerProperty(name, null);
  }

  public Integer getIntegerProperty(String name, Integer defaultValue) {
    return getProperty(name, defaultValue);
  }
}